package com.library.verification.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.widget.TextView;

import com.library.verification.R;
import com.library.verification.manager.VerificationManager;
import com.library.verification.utils.Tools;

/**
 * Created by zhoulikai on 17/4/24.
 * 验证码提示
 */

public class TipTextView extends TextView {
    private final String[] tip = {
            "请输入正确的图片验证码",
            "输入正确"
    };

    public enum CheckStatus {
        SUCCESS,
        START,
        FAIL
    }

    public TipTextView(Context context) {
        super(context);
    }

    public TipTextView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public TipTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public TipTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    public void show(CheckStatus checkStatus) {
        if (checkStatus == CheckStatus.SUCCESS) {
            this.setText(tip[1]);
            this.setTextColor(0xff545454);
            this.setTipImage(R.drawable.correct);
        } else if (checkStatus == CheckStatus.FAIL) {
            this.setText(tip[0]);
            this.setTextColor(0xfff44646);
            this.setTipImage(R.drawable.fail);
        } else if (checkStatus == CheckStatus.START) {
            this.setText(tip[0]);
            this.setTextColor(0xfff44646);
            this.setTipImage(R.drawable.fail);
        }
    }

    private void setTipImage(int id) {
        Drawable tip = getResources().getDrawable(id);
        tip.setBounds(0, 0, Tools.dip2px(this.getContext(), 13), Tools.dip2px(this.getContext(), 13));
        this.setCompoundDrawablePadding(Tools.dip2px(this.getContext(), 5));
        this.setCompoundDrawables(tip, null, null, null);
    }

    public void setVerificationManagerParameter(VerificationManager.VerificationManagerParameter verificationManagerParameter) {
        if (verificationManagerParameter != null) {
            boolean isShow = verificationManagerParameter.isInsideTextTip();
            if (isShow) {
                this.setVisibility(VISIBLE);
            } else {
                this.setVisibility(GONE);
            }
        }
    }
}
